package com.my.oauth2.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/*Security配置*/
@Configuration
@EnableWebSecurity
//,1: 加载了WebSecurityConfiguration配置类, 配置安全认证策略。2: 加载了AuthenticationConfiguration, 配置了认证信息。
public class SecurityConfig  extends WebSecurityConfigurerAdapter {

    /*指定加密方式*/
    @Bean
    public PasswordEncoder getPw(){
        return new BCryptPasswordEncoder();
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //授权认证
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers(
                        "/error.html",
                        "/login.html",
                        "/oauth/**",
                        "logout/**"
                ).permitAll()
                //所有请求都必须认证-登录之后访问
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .permitAll();
    }

    /**
     * 密码模式才需要
     * @return
     * @throws Exception
     */
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
//    @Resource
//    private DataSource dataSource;
//    @Bean
//    public ClientDetailsService clientDetails() {
//        return new JdbcClientDetailsService(dataSource);
//    }

}
